package pay;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.sys.SysConfigPayMemory;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.order.OrderInfo;
import o2o.dao.PO.sys.SysConfigPay;
import o2o.system.data.SYS_SETTINGS;
import pay.alipay.AliPayUtils;
import pay.config.Constant;
import pay.config.WeixinConfig;
import pay.dao.QueryResult;
import pay.dao.WX_ALI_TRI_CHANNEL_TYPE;
import pay.exceptions.DataEmptyException;
import pay.exceptions.RepeatPayExcetion;
import pay.exceptions.RequestFailException;
import pay.weixin.WeixinPayUtils;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: Jahnke [1029777564@qq.com]
 * Date: 2017-01-16 10:55
 * 支付
 */
public class PayUtils {
    private PayUtils() {
    }

    private static PayUtils single = new PayUtils();

    public static PayUtils getInstance() {
        if (single == null) {
            single = new PayUtils();
        }
        return single;
    }

    // 获取支付签名
    public static String getSign(String sysAppKey, String appFlag, String orderNo, String payType, Double price, String wxOpenId) throws Exception {
        String sign = null;
        switch (payType) {
            case ChannelUtils.alipay: {//支付宝
                sign = AliPayUtils.getInstance().app_pay(orderNo, Float.parseFloat(price + ""), "30m", Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
            }
            break;
            case ChannelUtils.wx: {//微信
                sign = WeixinPayUtils.getInstance().mobile_direct_pay(orderNo, price, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
            }
            break;
            case ChannelUtils.wx_xcx: {//微信小程序支付
                if (ValidateUtil.isNull(wxOpenId)) {
                    throw new Exception("未取得该用户的微信openId无法支付！");
                }
                sign = WeixinPayUtils.getInstance().winxin_pay_xcx(sysAppKey, appFlag, wxOpenId, orderNo, price, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
            }
            break;
            case ChannelUtils.wx_gzh: {//微信公众号支付
                //获取用户的openid
                String openId = wxOpenId;
                if (ValidateUtil.isNull(openId)) {
                    return AjaxResult.errorResult("未取得该用户的微信openId无法支付");
                }
                try {
                    sign = WeixinPayUtils.getInstance().winxin_pay_gzh(openId, orderNo, price, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (RepeatPayExcetion repeatPayExcetion) {
                    repeatPayExcetion.printStackTrace();
                }
            }
            break;
            default:
                System.out.println("支付方式错误！");
                throw new Exception("支付方式错误！");
        }

        return sign;
    }

    //检测支付状态
//    public synchronized String getTradeStatus(String channel, String orderNo, String payOrderNo) throws QueryExcetion, AlipayApiException, JDOMException, DataEmptyException, IOException {
//        String tradeStatus = "";
//        //根据订单请求最新的对应的支付渠道判断是否支付
//        if (channel.equals(ChannelUtils.alipay) || channel.equals(ChannelUtils.alipay_qr)) {
//            //访问支付宝--获取订单状态
//            tradeStatus = AliPayUtils.getInstance().pay_trade_query("", orderNo);
//        } else if (channel.equals(ChannelUtils.wx) || channel.equals(ChannelUtils.wx_pub_qr)) {
//            //访问微信的接口--获取订单状态
//            tradeStatus = WeixinPayUtils.getInstance().query_pay_trade(payOrderNo, orderNo);
//        }
//        return tradeStatus;
//    }

    //检测支付状态
    public static QueryResult getTradeStatusQ(String appFlag, String channel, String orderNo, String payOrderNo) {
        return getTradeStatusQ(null, appFlag, channel, UserType.USER, orderNo, payOrderNo);
    }

    public static QueryResult getTradeStatusQ(Long agentId, String channel, Integer userType, String orderNo, String payOrderNo) {
        return getTradeStatusQ(agentId, "", channel, userType, orderNo, payOrderNo);
    }

    public static QueryResult getTradeStatusQ(Long agentId, String appFlag, String channel, Integer userType, String orderNo, String payOrderNo) {
        QueryResult queryResult = new QueryResult();
        if (ValidateUtil.isNull(channel)) {
            System.out.println("查询支付，支付渠道为空");
            return null;
        }
        //根据订单请求最新的对应的支付渠道判断是否支付
        if (channel.equals(ChannelUtils.alipay) || channel.equals(ChannelUtils.alipay_qr)) {
            // 获取支付配置信息
            if (ValidateUtil.isID(agentId)) {
                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(agentId, appFlag);
                if (null != sysConfigPay) {
                    //访问支付宝--获取订单状态
                    queryResult = AliPayUtils.getInstance().getAliPayStatus("", orderNo,
                            sysConfigPay.getAliAppId(), sysConfigPay.getAliPrivateKey(),
                            sysConfigPay.getAliPublicKey(), sysConfigPay.getAliSignType());
                } else {
                    //访问支付宝--获取订单状态
//                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == 1 - SYS_SETTINGS.WX_ALI_USE_TRI) {
                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
//                        queryResult = SynoPayUtils.getOrderStatus(payOrderNo);// getTradeStatusQ
                        queryResult = WxAliTriPayUtils.triQueryPay(agentId, channel, userType, orderNo, payOrderNo);// getTradeStatusQ
                    } else {
                        queryResult = AliPayUtils.getInstance().getAliPayStatus("", orderNo);
                    }
                }
            } else {
//                //访问支付宝--获取订单状态
//                queryResult = AliPayUtils.getInstance().getAliPayStatus("", orderNo);
                //访问支付宝--获取订单状态
//                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == 1 - SYS_SETTINGS.WX_ALI_USE_TRI) {
                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
//                    queryResult = SynoPayUtils.getOrderStatus(payOrderNo);// getTradeStatusQ
                    queryResult = WxAliTriPayUtils.triQueryPay(agentId, channel, userType, orderNo, payOrderNo);
                } else {
                    queryResult = AliPayUtils.getInstance().getAliPayStatus("", orderNo);
                }
            }
        } else if (channel.equals(ChannelUtils.wx) || channel.equals(ChannelUtils.wx_pub_qr) || channel.equals(ChannelUtils.wx_gzh) || channel.equals(ChannelUtils.wx_xcx)) {
            //访问微信的接口--获取订单状态
            if (ValidateUtil.isID(agentId)) {
                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(agentId, appFlag);
//                if (null == sysConfigPay) {
//                    System.out.println("支付信息未配置，请联系管理员！");
//                    return null;
//                }

                //访问微信的接口--获取订单状态
                String appId = WeixinConfig.appid;
                String mchId = WeixinConfig.mch_id;
                String apiKey = WeixinConfig.apiKey;
                if (null != sysConfigPay) {
                    switch (channel) {
                        case ChannelUtils.wx: {//微信
                            if (null == userType) {
                                userType = UserType.USER;
                            }
                            switch (userType) {
                                case UserType.SHOP_OWNER: {// 店铺端
                                    appId = sysConfigPay.getWxShopAppId();
                                    mchId = sysConfigPay.getWxShopMchId();
                                    apiKey = sysConfigPay.getWxShopApiKey();
                                }
                                break;
                                case UserType.POSTMAN: {// 骑手端
                                    appId = sysConfigPay.getWxQsAppId();
                                    mchId = sysConfigPay.getWxQsMchId();
                                    apiKey = sysConfigPay.getWxQsApiKey();
                                }
                                break;
                                default: {// 用户
                                    appId = sysConfigPay.getWxAppId();
                                    mchId = sysConfigPay.getWxMchId();
                                    apiKey = sysConfigPay.getWxApiKey();
                                }
                                break;
                            }

                        }
                        break;
                        case ChannelUtils.wx_pub_qr:
                        case ChannelUtils.wx_gzh: {//微信公众号支付
                            appId = sysConfigPay.getWxGzhAppId();
                            mchId = sysConfigPay.getWxGzhMchId();
                            apiKey = sysConfigPay.getWxGzhApiKey();
                        }
                        break;
                        case ChannelUtils.wx_xcx: { //微信小程序支付
                            appId = sysConfigPay.getWxXcxAppId();
                            mchId = sysConfigPay.getWxXcxMchId();
                            apiKey = sysConfigPay.getWxXcxApiKey();
                        }
                        break;
                    }
                } else {
                    if (channel.equals(ChannelUtils.wx_pub_qr) || channel.equals(ChannelUtils.wx_gzh)) {
                        appId = WeixinConfig.gzhAppId;
                        mchId = WeixinConfig.gzhMchId;
                        apiKey = WeixinConfig.gzhApiKey;
                    } else if (channel.equals(ChannelUtils.wx_xcx)) {
                        appId = WeixinConfig.xcxAppId;
                        mchId = WeixinConfig.xcxMchId;
                        apiKey = WeixinConfig.xcxApiKey;
                        if (ValidateUtil.isNotNull(appFlag)) {
                            SysConfigPay sysConfigPay2 = SysConfigPayMemory.getInstance().getByKey(null, appFlag);
                            if (null != sysConfigPay2) {
                                appId = sysConfigPay2.getWxXcxAppId();
                                mchId = sysConfigPay2.getWxXcxMchId();
                                apiKey = sysConfigPay2.getWxXcxApiKey();
                            }
                        }
                    }
                }

              /*  queryResult = WeixinPayUtils.getInstance().getWxPayStatus(appId, mchId, apiKey,
                        payOrderNo, channel,
                        orderNo);*/

                //访问支付宝--获取订单状态
                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
                    queryResult = WxAliTriPayUtils.triQueryPay(agentId, channel, userType, orderNo, payOrderNo);
                } else {
                    queryResult = WeixinPayUtils.getInstance().getWxPayStatus(appId, mchId, apiKey,
                            payOrderNo, channel,
                            orderNo);
                }
            } else {
                //访问支付宝--获取订单状态
                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
                    queryResult = WxAliTriPayUtils.triQueryPay(agentId, channel, userType, orderNo, payOrderNo);
                } else {
                    queryResult = WeixinPayUtils.getInstance().getWxPayStatus(appFlag, payOrderNo, channel, userType, orderNo);
                }
            }
        }

        return queryResult;
    }

    //骑手端 检测支付状态
    public static QueryResult getQSTradeStatusQ(String channel, String orderNo, String payOrderNo) {
        QueryResult queryResult = new QueryResult();
        if (ValidateUtil.isNull(channel)) {
            System.out.println("查询支付，支付渠道为空");
            return null;
        }
        //根据订单请求最新的对应的支付渠道判断是否支付
        if (channel.equals(ChannelUtils.alipay) || channel.equals(ChannelUtils.alipay_qr)) {
            //访问支付宝--获取订单状态
            queryResult = AliPayUtils.getInstance().getAliPayStatus("", orderNo);
        } else if (channel.equals(ChannelUtils.wx) || channel.equals(ChannelUtils.wx_pub_qr) || channel.equals(ChannelUtils.wx_gzh) || channel.equals(ChannelUtils.wx_xcx)) {
            //访问微信的接口--获取订单状态
            queryResult = WeixinPayUtils.getInstance().getQsWxPayStatus(payOrderNo, channel, orderNo);
        }
        return queryResult;
    }

    public boolean refundPart(Long agentId, String appFlag, String channel, String orderNo, String backOrderNo, String money, String refundMoney, String chargeId, boolean isUpdateOrderRefund) {
        if (channel.equals(ChannelUtils.alipay) || channel.equals(ChannelUtils.alipay_qr) || channel.equals(ChannelUtils.alipay_wap)) {
            //访问支付宝--获取订单状态
            if (ValidateUtil.isID(agentId)) {
                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(agentId, appFlag);
                if (null == sysConfigPay) {
                    return AliPayUtils.getInstance().refundPart(orderNo, backOrderNo, refundMoney, isUpdateOrderRefund);
                } else {
                    return AliPayUtils.getInstance().refundPart(orderNo, backOrderNo, refundMoney,
                            sysConfigPay.getAliAppId(), sysConfigPay.getAliPrivateKey(),
                            sysConfigPay.getAliPublicKey(), sysConfigPay.getAliSignType(), isUpdateOrderRefund);
                }
            }
            return AliPayUtils.getInstance().refundPart(orderNo, backOrderNo, refundMoney, isUpdateOrderRefund);
        } else if (channel.equals(ChannelUtils.wx) || channel.equals(ChannelUtils.wx_pub_qr) || channel.equalsIgnoreCase(ChannelUtils.wx_xcx) || channel.equalsIgnoreCase(ChannelUtils.wx_pub_wap)) {
            try {
                String appId = WeixinConfig.appid;
                String mch_id = WeixinConfig.mch_id;
                String apiKey = WeixinConfig.apiKey;
                String certFilePath = WeixinConfig.wx_refund_cert_file_path;

                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(agentId, appFlag);
                if (null != sysConfigPay) {
                    switch (channel) {
                        case ChannelUtils.wx: {//微信
                            appId = sysConfigPay.getWxAppId();
                            mch_id = sysConfigPay.getWxMchId();
                            apiKey = sysConfigPay.getWxApiKey();
                            certFilePath = sysConfigPay.getWxCertFilePath();
                        }
                        break;
                        case ChannelUtils.wx_pub_qr:
                        case ChannelUtils.wx_gzh: {//微信公众号支付
                            appId = sysConfigPay.getWxGzhAppId();
                            mch_id = sysConfigPay.getWxGzhMchId();
                            apiKey = sysConfigPay.getWxGzhApiKey();
                            certFilePath = sysConfigPay.getWxGzhCertFilePath();
                        }
                        break;
                        case ChannelUtils.wx_xcx: { //微信小程序支付
                            appId = sysConfigPay.getWxXcxAppId();
                            mch_id = sysConfigPay.getWxXcxMchId();
                            apiKey = sysConfigPay.getWxXcxApiKey();
                            certFilePath = sysConfigPay.getWxXcxCertFilePath();
                        }
                        break;
                    }
                } else {
                    if (channel.equals(ChannelUtils.wx_pub_qr) || channel.equals(ChannelUtils.wx_gzh)) {
                        appId = WeixinConfig.gzhAppId;
                        mch_id = WeixinConfig.gzhMchId;
                        apiKey = WeixinConfig.gzhApiKey;
                        certFilePath = WeixinConfig.wx_gzh_refund_cert_file_path;
                    } else if (channel.equals(ChannelUtils.wx_xcx)) {
                        appId = WeixinConfig.xcxAppId;
                        mch_id = WeixinConfig.xcxMchId;
                        apiKey = WeixinConfig.xcxApiKey;
                        certFilePath = WeixinConfig.wx_xcx_refund_cert_file_path;
                        if (ValidateUtil.isNotNull(appFlag)) {
                            SysConfigPay sysConfigPay2 = SysConfigPayMemory.getInstance().getByKey(null, appFlag);
                            if (null != sysConfigPay2) {
                                appId = sysConfigPay2.getWxXcxAppId();
                                mch_id = sysConfigPay2.getWxXcxMchId();
                                apiKey = sysConfigPay2.getWxXcxApiKey();
                                certFilePath = sysConfigPay2.getWxXcxCertFilePath();
                            }
                        }
                    }
                }
                return WeixinPayUtils.getInstance().refundPart(appId, mch_id, apiKey,
                        certFilePath,
                        orderNo, backOrderNo, Double.parseDouble(money), Double.parseDouble(refundMoney), isUpdateOrderRefund);
            } catch (RequestFailException e) {
//                e.printStackTrace();
                System.out.println("微信退款失败1：" + e.getMessage());
                return false;
            } catch (DataEmptyException e) {
                System.out.println("微信退款失败2：" + e.getMessage());
//                e.printStackTrace();
                return false;
            }
        }
        return false;
    }


    /**
     * @param channel
     * @param orderNo   退款订单号
     * @param fcOrderNo 当前系统中的订单号
     * @param money
     * @param chargeId
     * @return
     */
//    public boolean refund(String channel, String orderNo, String fcOrderNo, String money, String chargeId) {
//        if (channel.equals(ChannelUtils.alipay) || channel.equals(ChannelUtils.alipay_qr) || channel.equals(ChannelUtils.alipay_wap)) {
//            //访问支付宝--获取订单状态
//            return AliPayUtils.getInstance().refund(orderNo, fcOrderNo, money);
//        } else if (channel.equals(ChannelUtils.wx) || channel.equals(ChannelUtils.wx_pub_qr) || channel.equalsIgnoreCase(ChannelUtils.wx_xcx) || channel.equalsIgnoreCase(ChannelUtils.wx_pub_wap)) {
//            //访问微信的接口--获取订单状态
//            try {
//                String appId = WeixinConfig.appid;
//                String mch_id = WeixinConfig.mch_id;
//                String apiKey = WeixinConfig.apiKey;
//                String certFilePath = WeixinConfig.wx_refund_cert_file_path;
//                if (channel.equals(ChannelUtils.wx_pub_qr) || channel.equals(ChannelUtils.wx_gzh)) {
//                    appId = WeixinConfig.gzhAppId;
//                    mch_id = WeixinConfig.gzhMchId;
//                    apiKey = WeixinConfig.gzhApiKey;
//                    certFilePath = WeixinConfig.wx_gzh_refund_cert_file_path;
//                } else if (channel.equals(ChannelUtils.wx_xcx)) {
//                    appId = WeixinConfig.xcxAppId;
//                    mch_id = WeixinConfig.xcxMchId;
//                    apiKey = WeixinConfig.xcxApiKey;
//                    certFilePath = WeixinConfig.wx_xcx_refund_cert_file_path;
//
////                    OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
//                    OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(ValidateUtil.isNull(fcOrderNo) ? orderNo : fcOrderNo);
//                    if (null != orderInfo && ValidateUtil.isID(orderInfo.getAgentId())) {
//                        SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(orderInfo.getAgentId());
//                        if (null != sysConfigPay && ValidateUtil.isNotNull(sysConfigPay.getWxXcxApiKey())) {
//                            appId = sysConfigPay.getWxXcxAppId();
//                            mch_id = sysConfigPay.getWxXcxMchId();
//                            apiKey = sysConfigPay.getWxXcxApiKey();
//                            certFilePath = sysConfigPay.getWxXcxCertFilePath();
//                        }
//                    }
//                }
//                return WeixinPayUtils.getInstance().refund(appId, mch_id, apiKey,
//                        certFilePath,
//                        orderNo, fcOrderNo, Double.parseDouble(money), Double.parseDouble(money));
//            } catch (RequestFailException e) {
////                e.printStackTrace();
//                System.out.println("微信退款失败1：" + e.getMessage());
//                return false;
//            } catch (DataEmptyException e) {
//                System.out.println("微信退款失败2：" + e.getMessage());
////                e.printStackTrace();
//                return false;
//            }
//        } else if (channel.equalsIgnoreCase(ChannelUtils.paypal)) {
//            if (ValidateUtil.isNull(chargeId)) {
//                System.out.println("paypal退款，信息不完整无法退款" + orderNo);
//                return false;
//            }
//            ResultInfo resultInfo = PaypalPayUtils.refund(Double.parseDouble(money), chargeId);
//            if (null != resultInfo) {
//                if (!resultInfo.isSuccess()) {
//                    System.out.println(resultInfo.getError() + "," + orderNo);
//                    return false;
//                }
//            } else {
//                System.out.println("paypal退款失败，" + orderNo);
//            }
//            return true;
//        } else if (channel.equalsIgnoreCase(ChannelUtils.apacpay)) {
//            return ApacpayUtils.refund(orderNo);
//        }
//
//        return false;
//    }

    /**
     * @param channel
     * @param orderNo   退款订单号
     * @param fcOrderNo 当前系统中的订单号
     * @param money
     * @param chargeId
     * @return
     */
    public ResultInfo refundV2(Long agentId, String appFlag, String channel, String orderNo, String fcOrderNo, String money, Double originalRefundMoney, String chargeId) {
        if (channel.equals(ChannelUtils.alipay) || channel.equals(ChannelUtils.alipay_qr) || channel.equals(ChannelUtils.alipay_wap)) {
            //访问支付宝--获取订单状态
            if (ValidateUtil.isID(agentId)) {
                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(agentId, appFlag);
                if (null == sysConfigPay) {
//                    return AliPayUtils.getInstance().refundV2(orderNo, fcOrderNo, money);
//                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == 1 - SYS_SETTINGS.WX_ALI_USE_TRI) {
                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
//                        ResultInfo resultInfo = SynoPayUtils.refund(chargeId, Double.parseDouble(money), fcOrderNo);
                        ResultInfo resultInfo = WxAliTriPayUtils.triRefund(agentId, channel, orderNo, fcOrderNo, money, originalRefundMoney, chargeId);
                        return resultInfo;
                    } else {
                        ResultInfo resultInfo = AliPayUtils.getInstance().refundV2(orderNo, fcOrderNo, money);
                        return resultInfo;
                    }
                } else {
                    return AliPayUtils.getInstance().refundV2(null, orderNo, fcOrderNo, money,
                            sysConfigPay.getAliAppId(), sysConfigPay.getAliPrivateKey(),
                            sysConfigPay.getAliPublicKey(), sysConfigPay.getAliSignType());
                }
            } else {
//                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == 1 - SYS_SETTINGS.WX_ALI_USE_TRI) {
                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
//                    ResultInfo resultInfo = SynoPayUtils.refund(chargeId, Double.parseDouble(money), fcOrderNo);
                    ResultInfo resultInfo = WxAliTriPayUtils.triRefund(agentId, channel, orderNo, fcOrderNo, money, originalRefundMoney, chargeId);
                    return resultInfo;
                } else {
                    ResultInfo resultInfo = AliPayUtils.getInstance().refundV2(orderNo, fcOrderNo, money);
                    return resultInfo;
                }
            }
        } else if (channel.equals(ChannelUtils.wx) || channel.equals(ChannelUtils.wx_pub_qr) || channel.equalsIgnoreCase(ChannelUtils.wx_xcx) || channel.equalsIgnoreCase(ChannelUtils.wx_pub_wap)) {
            //访问微信的接口--获取订单状态
            try {
                String appId = WeixinConfig.appid;
                String mch_id = WeixinConfig.mch_id;
                String apiKey = WeixinConfig.apiKey;
                String certFilePath = WeixinConfig.wx_refund_cert_file_path;

                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(agentId, appFlag);
                if (null != sysConfigPay) {
                    switch (channel) {
                        case ChannelUtils.wx: {//微信
                            appId = sysConfigPay.getWxAppId();
                            mch_id = sysConfigPay.getWxMchId();
                            apiKey = sysConfigPay.getWxApiKey();
                            certFilePath = sysConfigPay.getWxCertFilePath();
                        }
                        break;
                        case ChannelUtils.wx_pub_qr:
                        case ChannelUtils.wx_gzh: {//微信公众号支付
                            appId = sysConfigPay.getWxGzhAppId();
                            mch_id = sysConfigPay.getWxGzhMchId();
                            apiKey = sysConfigPay.getWxGzhApiKey();
                            certFilePath = sysConfigPay.getWxGzhCertFilePath();
                        }
                        break;
                        case ChannelUtils.wx_xcx: { //微信小程序支付
                            appId = sysConfigPay.getWxXcxAppId();
                            mch_id = sysConfigPay.getWxXcxMchId();
                            apiKey = sysConfigPay.getWxXcxApiKey();
                            certFilePath = sysConfigPay.getWxXcxCertFilePath();
                        }
                        break;
                    }
                } else {
                    if (channel.equals(ChannelUtils.wx_pub_qr) || channel.equals(ChannelUtils.wx_gzh)) {
                        appId = WeixinConfig.gzhAppId;
                        mch_id = WeixinConfig.gzhMchId;
                        apiKey = WeixinConfig.gzhApiKey;
                        certFilePath = WeixinConfig.wx_gzh_refund_cert_file_path;
                    } else if (channel.equals(ChannelUtils.wx_xcx)) {
                        appId = WeixinConfig.xcxAppId;
                        mch_id = WeixinConfig.xcxMchId;
                        apiKey = WeixinConfig.xcxApiKey;
                        certFilePath = WeixinConfig.wx_xcx_refund_cert_file_path;

                        if (ValidateUtil.isNotNull(appFlag)) {
                            SysConfigPay sysConfigPay2 = SysConfigPayMemory.getInstance().getByKey(null, appFlag);
                            if (null != sysConfigPay2) {
                                appId = sysConfigPay2.getWxXcxAppId();
                                mch_id = sysConfigPay2.getWxXcxMchId();
                                apiKey = sysConfigPay2.getWxXcxApiKey();
                                certFilePath = sysConfigPay2.getWxXcxCertFilePath();
                            }
                        }
                    }
                }

//                boolean re = WeixinPayUtils.getInstance().refund(appId, mch_id, apiKey,
//                        certFilePath,
//                        orderNo, fcOrderNo, Double.parseDouble(money), Double.parseDouble(money));
//                ResultInfo resultInfo = new ResultInfo();
//                resultInfo.setSuccess(re);
//                return resultInfo;

//                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == 1 - SYS_SETTINGS.WX_ALI_USE_TRI) {
                if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
//                    ResultInfo resultInfo = SynoPayUtils.refund(chargeId, Double.parseDouble(money), fcOrderNo);
                    ResultInfo resultInfo = WxAliTriPayUtils.triRefund(agentId, channel, orderNo, fcOrderNo, money, originalRefundMoney, chargeId);
                    return resultInfo;
                } else {
                    boolean re = WeixinPayUtils.getInstance().refund(appId, mch_id, apiKey,
                            certFilePath,
                            orderNo, fcOrderNo, Double.parseDouble(money), Double.parseDouble(money));
                    ResultInfo resultInfo = new ResultInfo();
                    resultInfo.setSuccess(re);
                    return resultInfo;
                }

            } catch (RequestFailException e) {
//                e.printStackTrace();
                System.out.println("微信退款失败1：" + e.getMessage());
                return new ResultInfo(e.getMessage());
//                return false;
            } catch (DataEmptyException e) {
                System.out.println("微信退款失败2：" + e.getMessage());
//                e.printStackTrace();
//                return false;
                return new ResultInfo(e.getMessage());
            }
        }

//        return false;
        return new ResultInfo("退款失败，请稍后再试");
    }

}
